package com.cjm.algorithm.tree;

import com.cjm.algorithm.TreeNode;

public class Ti404 {
    public static void main(String[] args) {
        Ti404 ti404 = new Ti404();
//        System.err.println(ti404.sumOfLeftLeaves(TreeNode.generatorTreeNode(new Integer[]{1, 2, 3, 4, 5})));
        System.err.println(ti404.sumOfLeftLeaves(TreeNode.generatorTreeNode(new Integer[]{3,9,20,null,null,15,7})));
    }

    public int sumOfLeftLeaves(TreeNode root) {
        return localsum(root, 0, "");
    }

    private int localsum(TreeNode root, int currentValue, String direction) {
        if (root == null) {
            return currentValue;
        }
        int cc = currentValue;
        if ("left".equals(direction) && root.left == null && root.right == null) {
            cc += root.val;
            return cc;
        }


        if (root.left != null) {
            cc = localsum(root.left, cc, "left");
        }
        if (root.right != null) {
            cc = localsum(root.right, cc, "right");
        }
        return cc;
    }
}
